package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分类与属性管理控制层
 * @author 搭忆长安
 * @version 1.0
 * @date 2021年06月26日 17:12
 * @ClassName:ManageController
 * @Description: TODO
 */
@Api(value = "ManageController",tags = "分类与属性管理控制层")
@RestController
@RequestMapping("/admin/product")
public class ManageController {
    @Autowired
    private ManageService manageService;

    /**
     * 查询所有一级分类列表
     *
     * @return
     */
    @ApiOperation("查询所有一级分类列表")
    @GetMapping(value = "/getCategory1")
    public Result<List<BaseCategory1>> getCategory1List() {
        List<BaseCategory1> category1List = manageService.getCategory1List();
        return Result.ok(category1List);
    }

    /**
     * 根据一级分类查询二级分类列表
     *
     * @param category1Id
     * @return
     */
    @ApiOperation("根据一级分类查询二级分类列表")
    @GetMapping(value = "/getCategory2/{category1Id}")
    public Result<List<BaseCategory2>> getCategory2List(@PathVariable(value = "category1Id") Long category1Id) {
        List<BaseCategory2> category2List = manageService.getCategory2List(category1Id);
        return Result.ok(category2List);
    }

    /**
     * 根据二级分类查询三级分类列表
     *
     * @param category2Id
     * @return
     */
    @ApiOperation("根据二级分类查询三级分类列表")
    @GetMapping(value = "/getCategory3/{category2Id}")
    public Result<List<BaseCategory3>> getCategory3List(@PathVariable(value = "category2Id") Long category2Id) {
        List<BaseCategory3> category3List = manageService.getCategory3List(category2Id);
        return Result.ok(category3List);
    }

    /**
     * 根据分类查询所有规格信息列表
     *
     * @param category1Id
     * @param category2Id
     * @param category3Id
     * @return
     */
    @ApiOperation("根据分类查询所有规格信息列表")
    @GetMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result<List<BaseAttrInfo>> getBaseAttrInfoList(@PathVariable(value = "category1Id") Long category1Id,
                                                          @PathVariable(value = "category2Id") Long category2Id,
                                                          @PathVariable(value = "category3Id") Long category3Id) {
        List<BaseAttrInfo> baseAttrInfoList = manageService.getBaseAttrInfoList(category1Id, category2Id, category3Id);
        return Result.ok(baseAttrInfoList);
    }

    /**
     * 添加平台属性信息
     *
     * @param baseAttrInfo
     * @return
     */
    @PostMapping("/saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo) {
        baseAttrInfo = manageService.addAttrInfo(baseAttrInfo);
        return Result.ok(baseAttrInfo);
    }

    /**
     * 通过平台属性id查询平台属性值的列表信息
     *
     * @param id
     * @return
     */
    @GetMapping("/getAttrValueList/{id}")
    public Result<List<BaseAttrValue>> getAttrValueList(@PathVariable(value = "id") Long id) {
        List<BaseAttrValue> baseAttrValues = manageService.getValueById(id);
        return Result.ok(baseAttrValues);
    }

    /**
     * @param category3Id
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/{page}/{size}")
    public Result<IPage<SpuInfo>> getSpuInfoList(Long category3Id,
                                                 @PathVariable(value = "page") Integer page,
                                                 @PathVariable(value = "size") Integer size) {
        IPage<SpuInfo> spuInfoList = manageService.getSpuInfoList(category3Id, page, size);
        return Result.ok(spuInfoList);
    }

    /**
     * 分页查询品牌
     *
     * @param size
     * @param page
     * @return
     */
    @GetMapping("/baseTrademark/{page}/{size}")
    public Result<IPage<BaseTrademark>> baseTradeMark(@PathVariable(value = "size") Integer size,
                                                      @PathVariable(value = "page") Integer page) {
        //获取分页品牌信息
        IPage<BaseTrademark> baseTrademarkIPage = manageService.getTrademarkList(page, size);
        return Result.ok(baseTrademarkIPage);
    }

    /**
     * 添加品牌
     *
     * @param baseTrademark
     * @return
     */
    @PostMapping(value = "/baseTrademark/saveBaseTrademark")
    public Result saveBaseTrademark(@RequestBody BaseTrademark baseTrademark) {
        //添加品牌
        manageService.addBaseTrademark(baseTrademark);
        return Result.ok();
    }

    /**
     * 根据id查询品牌
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/baseTrademark/get/{id}")
    public Result saveBaseTrademark(@PathVariable(value = "id") Long id) {
        BaseTrademark trademark = manageService.getBaseTrademarkById(id);
        return Result.ok(trademark);
    }

    /**
     * 添加品牌
     *
     * @param baseTrademark
     * @return
     */
    @PutMapping(value = "/baseTrademark/update")
    public Result update(@RequestBody BaseTrademark baseTrademark) {
        manageService.editBaseTrademark(baseTrademark);
        return Result.ok();
    }

    /**
     * 删除品牌
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/baseTrademark/remove/{id}")
    public Result delete(@PathVariable(value = "id") Long id) {
        manageService.deleteBaseTrademark(id);
        return Result.ok();
    }

    /**
     * 查询所有的销售属性
     *
     * @return
     */
    @GetMapping("/baseSaleAttrList")
    public Result<List<BaseSaleAttr>> baseSaleAttrList() {
        List<BaseSaleAttr> baseSaleAttr = manageService.getBaseSaleAttr();
        return Result.ok(baseSaleAttr);
    }

    /**
     * 获取品牌的所有信息
     *
     * @return
     */
    @GetMapping("/baseTrademark/getTrademarkList")
    public Result<List<BaseTrademark>> baseTrademark() {
        List<BaseTrademark> trademark = manageService.getTrademark();
        return Result.ok(trademark);
    }

    /**
     * 添加
     *
     * @param spuInfo
     * @return
     */
    @PostMapping(value = "/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo) {
        spuInfo = manageService.saveSpuInfo(spuInfo);
        return Result.ok(spuInfo);
    }

    /**
     * 查询所有的spu图片列表
     *
     * @param id
     * @return
     */
    @GetMapping("/spuImageList/{id}")
    public Result<List<SpuImage>> spuImageList(@PathVariable(value = "id") Long id) {
        List<SpuImage> spuImages = manageService.getSpuImageList(id);
        return Result.ok(spuImages);
    }

//    /**
//     * 查询平台属性信息
//     * @param category1
//     * @param category2
//     * @param category3
//     * @return
//     */
//    @GetMapping("/attrInfoList/{category1}/{category2}/{category3}")
//    public Result<List<BaseAttrInfo>> getBaseAttrInfo(@PathVariable(value = "category1")Long category1,
//                                                      @PathVariable(value = "category2")Long category2,
//                                                      @PathVariable(value = "category3")Long category3){
//        List<BaseAttrInfo> baseAttrInfos = manageService.getBaseAttrInfoList(category1, category2, category3);
//        return Result.ok(baseAttrInfos);
//    }

    /**
     * spuId查询销售属性列表
     *
     * @param id
     * @return
     */
    @GetMapping("/spuSaleAttrList/{id}")
    public Result<List<SpuSaleAttr>> spuSaleAttrList(@PathVariable(value = "id") Long id) {
        List<SpuSaleAttr> spuSaleAttrList = manageService.getSpuSaleAttrList(id);
        return Result.ok(spuSaleAttrList);
    }

    /**
     * 添加spu
     *
     * @param skuInfo
     * @return
     */
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo) {
        skuInfo = manageService.saveSkuInfo(skuInfo);
        return Result.ok(skuInfo);
    }

    /**
     * 分页查询skuinfo信息
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/list/{page}/{size}")
    public Result<IPage<SkuInfo>> list(@PathVariable(value = "page") Integer page, @PathVariable(value = "size") Integer size) {
        IPage<SkuInfo> list = manageService.list(page, size);
        return Result.ok(list);
    }

    /**
     * sku下架
     *
     * @param id
     * @return
     */
    @GetMapping("/cancelSale/{id}")
    public Result cancelSale(@PathVariable(value = "id") Long id) {
        manageService.UpOrDown(id, 0);
        return Result.ok();
    }

    /**
     * sku上架
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/onSale/{id}")
    public Result onSale(@PathVariable(value = "id") Long id) {
        manageService.UpOrDown(id, 1);
        return Result.ok();
    }

}
